home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Demos / Tools / SoundMusicSys / Source Examples / SoundMusicSystem.h < prev   
Encoding:
Text File  |  1995-03-29  |  14.4 KB  |  329 lines  |  [TEXT/KAHL]

  1. /*****************************************************/
  2. /*
  3. **    SoundMusicSys.h
  4. **
  5. **        Structures for handling the sounds used by system
  6. **
  7. **          (c) 1989-1995 by Steve Hales, All Rights Reserved
  8. **  
  9. **
  10. **    History    -
  11. **     7/20/89    Created
  12. **     7/24/89    Created Asyncronous Sample List Player.
  13. **     12/3/89    Modified all tools to work with the Sound Driver instead of the
  14. **            Sound Manager (He got fired.)
  15. **    12/21/89 Added PlayTheSample.
  16. **    2/1/90    Added Sound Manager (he got hired as a consultant) and use it
  17. **            when system 6.0 or greater is around. (Better sound.   !!?!??)
  18. **    4/10/90    Fixed pause/resume sound to allocate/deallocate sound channel.
  19. **    7/3/90    Added a REST mechanisim to the SampleList player
  20. **    7/28/90    Added IsSoundListFinished() to the SampleList player
  21. **    7/31/90    Added IsSoundFXFinished()
  22. **    8/13/90    Moved example and StandAlone code to a seperate project.
  23. **  12/12/90    Added GetSoundWaveform() & GetSoundLength() & GetSoundDefaultRate().
  24. **    1/9/91    Added sound done call back function.
  25. **    3/4/91    Completed work on Sound Driver's call back.
  26. **    4/10/91    Fixed a bug in CalcPlaybackLength that calculated the wrong length.
  27. **    4/10/91    Added purgability of the samples.
  28. **    4/23/91    Added better compatibility to Think C version 3.0
  29. **    4/30/91    Put in support for snd resource type 1 and type 2.
  30. **    5/18/91    Changed to zone calls instead of globals.
  31. **    5/20/91    Mucking around for bug that Mick is having.
  32. **    5/20/91    Added csnd compressed resource based upon the LZSS algo.
  33. **    5/25/91    Added a FinisSoundChannel/InitSoundChannel inside of EndSound() for Mick
  34. **    5/27/91    Fixed potential bug with BeginSoundList() in which the sound may be
  35. **            loaded during a VBL!
  36. **    5/28/91    Modified to new Think C MPW like header names.
  37. **    6/18/91    Added delay to the starting of a new sound with BeginSound (4 Ticks).
  38. **    7/24/91    Added delay to the starting of a new sound with PlayTheSample (4 Ticks).
  39. **    8/5/91    Removed duplicate code in BeginSound & PlayTheSample.
  40. **    8/5/91    Added 2 tick delay to soundLength. Used in IsSoundFXFinished.
  41. **    8/5/91    Added BeginSoundReverse & BeginSoundSection.
  42. **    8/6/91    Fixed some memory management problems with SoundMemorySize.
  43. **    8/6/91    Added a semaphore to the vbl task.
  44. **    9/9/91    Cleaned up for Think C 5.0
  45. **    9/9/91    Added PLAY_ALL_SAMPLE
  46. **    9/9/91    Changed all 'int's to 'short int'. Sorry guys. This is because I want
  47. **            16 bit integers.
  48. **    10/8/91    Added sound looping features. BeginSoundLoop(), BeginSoundEnvelope(),
  49. **            and BeginSoundEnvelopeProc().
  50. **    11/11/91 Added SoundLock().
  51. **    1/22/92    Put in buffer SndDoCommands for looped & enveloped sounds
  52. **    3/9/92    Modifed for MPW and removed all THINK C 3 & 4 compiler stuff
  53. **    3/23/92    Added access to"Private-Sound/Music.h"
  54. **    5/1/92    Added the SoundChannelRestart() function
  55. **    7/10/92    Combined SoundSys.h & MusicSys.h into new and improved version
  56. **    7/10/92    Added new calls to initilize & clean up the system
  57. **    7/10/92    Heavily modified for Rev 3 of the sound system.
  58. **    7/10/92    Now calling jim's sound mixer
  59. **    7/13/92    Added BeginMasterFadeOut & FadeLevel
  60. **    7/18/92    Fully intergrated the SoundManager out and replaced with "SoundGod"
  61. **    7/26/92    Added ChangeOuputQuality() and fixed bug with BeginSongLooped
  62. **    7/31/92    Fixed weird bug with MPW passing of 32-bit value "shorts"!!
  63. **    7/31/92    Fixed BeginMasterFadeOut code.
  64. **    8/4/92    Change playback rate command to 16.16 fixed value. Sorry guys.
  65. **    8/6/92    Built a mult-voice tracker, to work with sound effects.
  66. **    8/23/92    Added 2 pt intrepolation    
  67. **    8/24/92    Added ChangeSystemVoices
  68. **    12/4/92    Added LoadSound
  69. **    12/9/92    Added FreeSound
  70. **    12/29/92 ResumeSoundMusicSystem and PauseSoundMusicSystem now return
  71. **            errors
  72. **    3/6/93    Fixed the SoundDone callback system.
  73. **    3/6/93    Elimated the playback decay govenour.
  74. **    3/16/93    Added BeginMasterFadeIn
  75. **    3/19/93    Added SetSongDoneCallBack
  76. **    5/31/93    Made LoadSong/FreeSong work
  77. **    6/14/93    Changed VBL task to be out of the system heap
  78. **    6/24/93    Added StartFilePlayback, EndFilePlayback, and ChangeFilePlaybackRate
  79. **    6/24/93    Added IsThisSoundFXFinished
  80. **    7/2/93    Added EndSoundList
  81. **    7/13/93    Fixed File streaming stop bug
  82. **    7/14/93    Worked on stopping callback bug
  83. **    7/15/93    Think I fixed the callback bug
  84. **    7/21/93    Forced callbacks to use a long for MPW
  85. **    7/25/93    Fixed bug with resume while file playback is in progress
  86. **    7/26/93    Changed all callbacks to be pascal based
  87. **    9/2/93    Fixed macro bug with FreeSong
  88. **    9/15/93    Fixed a looping bug with BeginSongLoop
  89. **    9/19/93    Exposed DeltaDecompressHandle for Larry, for those who want to decompress a 'csnd' resource
  90. **    10/27/93 Fixed bug with ReleaseRegisteredSounds that caused a crash
  91. **    11/18/93 Added ServiceFilePlayback to the API for StartFilePlayback
  92. **    11/18/93 Added GetSoundLoopStart & GetSoundLoopEnd
  93. **    11/18/93 Added SetMasterVolume & GetMasterVolume
  94. **    12/4/93    Added code/data cache flush to callback
  95. **    12/4/93    Added GetCurrentMidiClock() & SetCurrentMidiClock()
  96. **    1/11/94    Cleaned header up a bit.
  97. **    2/28/94    Added PlayTheSampleWithID
  98. **    3/15/94    Found a nasty callback problem and fixed it.
  99. **    4/5/94    Fixed memory leak with instrument loading
  100. **    4/29/94    Removed external div and mul function references for A4 land.
  101. **    6/23/94    Updated to CodeWarrior
  102. **    9/2/94    Added mixedmode manager support
  103. **    12/2/94    Added ChangeSoundVolume and ChangeFilePlaybackVolume
  104. **
  105. */
  106. /*****************************************************/
  107.  
  108.  
  109. #ifndef SOUNDMUSICSYS_DRIVER
  110. #define SOUNDMUSICSYS_DRIVER
  111.  
  112. #ifdef __cplusplus
  113. extern "C" {
  114. #endif
  115.  
  116. // SOUND DRIVER EQUATES
  117.  
  118. /* Definitions for Sound resources */
  119. #define SOUND_END                -1                    /* End Sound for list */
  120.  
  121. /* Defines for sound functions */
  122. #define SOUND_RATE_DEFAULT        0L                    /* Default rate */
  123. #define SOUND_RATE_FAST        0x56EE8BA3L            /* Fast */
  124. #define SOUND_RATE_MEDIUM        0x2B7745D1L            /* Medium */
  125. #define SOUND_RATE_MOSEY        SOUND_RATE_FAST/3    /* Mosey */
  126. #define SOUND_RATE_SLOW        SOUND_RATE_MEDIUM/2    /* Slow */
  127.  
  128. #define SOUND_RATE_22k        SOUND_RATE_FAST
  129. #define SOUND_RATE_11k        SOUND_RATE_MEDIUM
  130. #define SOUND_RATE_7k            SOUND_RATE_MOSEY
  131. #define SOUND_RATE_5k            SOUND_RATE_SLOW
  132.  
  133. #define REST_SAMPLE            -1L                /* When using the SampleList mechanisim, this will
  134.                                             ** rest (no sound) for the duration of the length
  135.                                             */
  136. #define PLAY_ALL_SAMPLE        -2L                /* When using the SampleList mechanisim, this will
  137.                                             ** play the entire sound if this is put into the SampleList
  138.                                             ** theLength parameter.
  139.                                             */
  140.  
  141. #define CUSTOM_PLAY_ID    ((short int)0x8000)        /* ID value of samples played with PlayTheSample */
  142. #define FILE_PLAY_1_ID        ((short int)0x8001)        /* ID value of samples played with StartFilePlayback (Buffer 1) */
  143. #define FILE_PLAY_2_ID        ((short int)0x8002)        /* ID value of samples played with StartFilePlayback (Buffer 1) */
  144.  
  145. /* Ranges for SetMasterFade() and FadeLevel() */
  146. #define MAX_VOLUME        0x100
  147. #define MIN_VOLUME        0
  148.  
  149. #define FULL_VOLUME        MAX_VOLUME
  150. #define NO_VOLUME            MIN_VOLUME
  151.  
  152. /* Structure for a list of samples to play asynchronously
  153. */
  154. #if _POWERPC_
  155. #pragma options align=mac68k
  156. #endif
  157. struct SampleList
  158. {
  159.     short int    theSampleID;        /* Sample resource ID or REST_SAMPLE for no sound */
  160.     long        theRate;            /* Sample playback rate */
  161.     short int    theLength;            /* Length of note in 1/60ths of a second */
  162. };
  163. typedef struct SampleList SampleList;
  164. #if _POWERPC_
  165. #pragma options align=reset
  166. #endif
  167.  
  168.  
  169. // SoundQuality
  170. #define jxNoDriver                0                // no driver
  171. #define jxAnalyzeQuality            1                // best driver for speed of mac
  172. #define jxLowQuality            2                // lowest quality (11khz) no intrepolation
  173. #define jxHighQuality            3                // high quality (22khz) no intrepolation
  174. #define jxIntrepLowQuality        4                // lowest quality with 2 pt intrepolation
  175. #define jxIntrepHighQuality        5                // high quality with 2 pt intrepolation
  176. #define jxIntrepBestLowQuality    6                // low quality with 128K memory intrepolation
  177. #define jxIntrepBestHighQuality    7                // high quality with 128K memory intrepolation
  178.  
  179. // Support for hystrical reasons. Bad spellings
  180. #define jxAnalyizeQuality            jxAnalyzeQuality
  181.  
  182. /* NOTE:
  183. **    For future revisions, the numbers will change into bit fields for features as we add
  184. **    support for 16 bit, stereo, etc. Use the defines to pass the values you want. If you 
  185. **    don't now, you may get in trouble in the future.
  186. */
  187. typedef short int SoundQuality;
  188.  
  189. /* FUNCTIONS
  190. */
  191. OSErr    InitSoundMusicSystem(short int maxSongVoices, 
  192.                             short int maxNormalizedVoices, 
  193.                             short int maxEffectVoices,
  194.                             SoundQuality quality);
  195. void        FinisSoundMusicSystem(void);
  196.  
  197. OSErr    ChangeSystemVoices    (short int maxSongVoices, 
  198.                             short int maxNormalizedVoices, 
  199.                             short int maxEffectVoices);
  200.  
  201. Boolean    IsSoundSystemPaused(void);        // just sound sub system
  202. Boolean    IsMusicSystemPaused(void);        // just music sub system
  203. Boolean    IsSoundMusicSystemPaused(void);    // everything
  204.  
  205. OSErr    PauseSoundMusicSystem(void);
  206. OSErr    ResumeSoundMusicSystem(void);
  207. OSErr    PauseMusicOnly(void);
  208. OSErr    ResumeMusicOnly(void);
  209.  
  210. Handle    DeltaDecompressHandle(Handle theData);
  211.  
  212. OSErr    RegisterSounds(short int *pSoundID, Boolean prePurge);
  213. void        ReleaseRegisteredSounds(void);
  214.  
  215. long        SoundMemorySize(short int *pSoundID);
  216.  
  217. void        ChangeSoundPitch(short int theID, long theRate);
  218. void        BeginSoundList(SampleList * sampleList, short int totalSamples);
  219. Boolean    IsSoundListFinished(void);
  220. void        ChangeSoundVolume(short int theID, short int theVolume);
  221. void        BeginSound(short int theID, long theRate);
  222. void        LoadSound(short int theID);
  223. void        FreeSound(short int theID);
  224. void        BeginSoundReverse(short int theID, long theRate);
  225. void        BeginSoundSection(short int theID, long theRate, long sectionStart, long sectionEnd);
  226. void        BeginSoundLoop(short int theID, long theRate, long loopStart, long loopEnd);
  227. void        BeginSoundEnvelope(short int theID, long theRate, short int loopTime);
  228. void        BeginSoundEnvelopeProc(short int theID, long theRate, pascal Boolean (*theLoopProc)(short int sampleID));
  229. Boolean    IsSoundFXFinished(void);
  230. Boolean    IsThisSoundFXFinished(short theID);
  231.  
  232. void        PlayTheSample(Ptr pSamp, long sampSize, long sampRate);
  233. void        PlayTheSampleWithID(Ptr pSamp, long sampSize, long sampRate, short int theID);
  234.  
  235. void        SetSoundDoneCallBack(pascal void (*theProcPtr)(short int theID));
  236. void        SetSongDoneCallBack(pascal void (*theProcPtr)(short int theID));
  237.  
  238. void        SetMasterVolume(short int theVolume);
  239. short int    GetMasterVolume(void);
  240.  
  241. void        SetSoundVBCallBack(pascal void (*theProc)(void));
  242.  
  243. Byte *    GetSoundWaveform(short int theID);
  244. long        GetSoundLength(short int theID);
  245. long        GetSoundDefaultRate(short int theID);
  246. long        GetSoundLoopStart(short int theID);
  247. long        GetSoundLoopEnd(short int theID);
  248. short int    GetSoundTime(short int theID, long sampRate);
  249. short int    CalcPlaybackLength(long theRate, long theLength);
  250. void        EndAllSound(void);
  251. void        EndSound(short int theID);
  252. void        EndSoundList(void);
  253. void        PurgeAllSounds(unsigned long minMemory);
  254.  
  255. OSErr    StartFilePlayback(FSSpec *theFile, long theRate, long bufferSize);
  256. Boolean    ServiceFilePlayback(void);
  257. void        ChangeFilePlaybackRate(long theNewRate);
  258. void        ChangeFilePlaybackVolume(short theVolume);
  259. void        EndFilePlayback(void);
  260.  
  261.  
  262. #define kBeginSong                    (short int)0    // kBeginSong, theSongID
  263. #define kBeginSongLooped                (short int)1    // kBeginSongLooped, theSongID
  264. #define kEndSong                    (short int)2    // kEndSong, 0
  265. #define kIsSongDone                    (short int)3    // kIsSongDone, 0
  266. #define kTicksSinceStart                (short int)4    // kTicksSinceStart, 0
  267. #define kPurgeFlag                    (short int)5    // kPurgeFlag, Boolean
  268. #define kLockFlag                    (short int)6    // kLockFlag, Boolean
  269. #define kLoopSongFlag                (short int)7    // kLoopSongFlag, Boolean
  270. #define kFadeOutput                    (short int)8    // kFadeOutput, level (0 to 256)
  271. #define kAutoFadeSoundOut            (short int)10    // kAutoFadeSoundOut, ticks for how fast
  272. #define kAutoFadeSoundIn                (short int)11    // kAutoFadeSoundIn, ticks for how fast
  273. #define kFadeLevel                    (short int)12    // kFadeLevel, short int
  274. #define kSetMasterFade                (short int)13    // kSetMasterFade, short int
  275. #define kChangeOutputQuality            (short int)14    // kChangeOutputQuality, SoundQuality
  276. #define kLoadSong                    (short int)15    // kLoadSong, theSongID
  277. #define kFreeSong                    (short int)16    // kFreeSong
  278. #define kGetErrors                    (short int)17    // kGetErrors, char *
  279. #define kGetMidiClock                (short int)18    // kGetMidiClock
  280. #define kGetMidiBeat                (short int)19    // kGetMidiBeat
  281. #define kGetNextMidiBeat                (short int)20    // kGetNextMidiBeat
  282. #define kSetMidiClock                (short int)21    // kSetMidiClock, pos
  283. #define kGetTempo                    (short int)22    // kGetTempo
  284. #define kGetSongLength                (short int)23    // kGetSongLength
  285. #define kGetSongVolume                (short int)24    // kGetSongVolume
  286. #define kSetSongVolume                (short int)25    // kSetSongVolume, volume
  287. #if !THINK_C
  288. #define MUSICDRIVERHANDLER MusicDriverHandler
  289. #endif
  290. pascal unsigned long MusicDriverHandler(short int theMessage, long theData);
  291.  
  292. // Function call interface:
  293.  
  294. #define    BeginSong(theSong)            MusicDriverHandler(kBeginSong, (long)(theSong))
  295. #define    BeginSongLooped(theSong)        MusicDriverHandler(kBeginSongLooped, (long)(theSong))
  296. #define    LoadSong(theSong)            MusicDriverHandler(kLoadSong, (long)(theSong))
  297. #define    FreeSong()                MusicDriverHandler(kFreeSong, 0L)
  298. #define    SetLoopSongFlag(theFlag)        MusicDriverHandler(kLoopSongFlag, (long)(theFlag))
  299. #define    EndSong()                    MusicDriverHandler(kEndSong, 0L)
  300. #define    IsSongDone()                MusicDriverHandler(kIsSongDone, 0L)
  301. #define    SongTicks()                MusicDriverHandler(kTicksSinceStart, 0L)
  302. #define    PurgeSongs(purge)            MusicDriverHandler(kPurgeFlag, (long)(purge))
  303. #define    LockSongs(lock)            MusicDriverHandler(kLockFlag, (long)(lock))
  304. #define    BeginMasterFadeOut(time)        MusicDriverHandler(kAutoFadeSoundOut, (long)(time))
  305. #define    BeginMasterFadeIn(time)        MusicDriverHandler(kAutoFadeSoundIn, (long)(time))
  306. #define    SetMasterFade(level)        MusicDriverHandler(kSetMasterFade, (long)(level))
  307. #define    FadeLevel()                MusicDriverHandler(kFadeLevel, 0L)
  308. #define    ChangeOuputQuality(qual)        MusicDriverHandler(kChangeOutputQuality, (long)(qual))
  309. #define    GetErrors(buffer)            MusicDriverHandler(kGetErrors, (long)(buffer))
  310. #define    GetCurrentMidiClock()        MusicDriverHandler(kGetMidiClock, 0L)
  311. #define    GetCurrentMidiBeat()        MusicDriverHandler(kGetMidiBeat, 0L)
  312. #define    GetNextBeatMidiClock()        MusicDriverHandler(kGetNextMidiBeat, 0L);
  313. #define    SetCurrentMidiClock(clock)    MusicDriverHandler(kSetMidiClock, (long)(clock))
  314. #define    GetCurrentTempo()            MusicDriverHandler(kGetTempo, 0L)
  315. #define    GetSongLength()            MusicDriverHandler(kGetSongLength, 0L)
  316. #define    SetSongVolume(volume)        MusicDriverHandler(kSetSongVolume, (long)(volume))
  317. #define    GetSongVolume()            MusicDriverHandler(kGetSongVolume, 0L)
  318.  
  319. OSErr    BeginSongFromMemory(short int theID, Handle theSongResource, Handle theMidiResource, Boolean loopSong);
  320. long        MaxVoiceLoad(void);
  321.  
  322.  
  323. #ifdef __cplusplus
  324. }
  325. #endif
  326.  
  327.  
  328. #endif    // SOUNDMUSICSYS_DRIVER
  329.